package com.amigo.online.common.base.security;

import com.amigo.online.common.param.result.LocalUserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class LocalUser {
	
	private static Logger logger = LoggerFactory.getLogger(LocalUser.class);
	
	private static ThreadLocal<LocalUserInfo> userThreadLocal = new ThreadLocal<LocalUserInfo>();
	
	public static LocalUserInfo getUser() {
		LocalUserInfo result = userThreadLocal.get();
		if(result == null) {
			logger.error("获取线程用户信息失败!当前线程名:" + Thread.currentThread().getId() + "");
		}
		return result;
	}
	
	public static void setUser(LocalUserInfo user) {		
		userThreadLocal.set(user);
		if(user != null) {
			/*logger.info("设置线程用户信息!当前线程名:" + Thread.currentThread().getId() + ",用户Token:" + user.getToken()
			+ ",用户名:"+ user.getUser().getNickname() + "------");*/
		} else {
			logger.error("设置线程用户信息为空!当前线程名:" + Thread.currentThread().getId() + "");
		}
		
	}
	
	public static void removeUser() {
		LocalUserInfo result = userThreadLocal.get();
		if(result == null) {
			return;
		} else {
		/*	logger.info("删除线程用户信息!当前线程名:" + Thread.currentThread().getId() +  ",用户Token:" + result.getToken()
			+ ",用户名:"+ result.getUser().getNickname() + "------");*/
		}	
		userThreadLocal.remove();	
	}
	public static void main(String[] args) {
		setUser(null);
	}
}
